package com.itheima.service.system.Impl;


import com.github.pagehelper.PageHelper;
import com.itheima.dao.system.UserDao;
import com.itheima.domain.system.User;
import com.itheima.factory.MapperFactory;
import com.itheima.service.system.UserService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class UserServiceImpl implements UserService {

    @Override
    public void save(User dept) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            String uuid = UUID.randomUUID().toString();
            dept.setId(uuid);
            mapper.save(dept);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException();
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }


    @Override
    public void delete(String id) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            mapper.delete(id);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException();
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public void update (User dept){
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            mapper.update(dept);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException();
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override public User findById (String id){
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            User byId = mapper.findById(id);
            TransactionUtil.commit(sqlSession);
            return byId;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException();
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public List<User> findAll (int page, int size){
        SqlSession sqlSession=null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            PageHelper.startPage(page, size);
            List<User> all = mapper.findAll();
            TransactionUtil.commit(sqlSession);
            return all;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException();
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public List<User> findAll() {
        SqlSession sqlSession=null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            List<User> all = mapper.findAll();
            TransactionUtil.commit(sqlSession);
            return all;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException();
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }
}
